package de.QuizCommunity;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.json.JSONException;
import org.json.JSONObject;

import com.facebook.android.FacebookError;
import com.facebook.android.Util;

import de.QuizCommunity.domain.Benutzer;
import de.QuizCommunity.util.AppVariabels;
import android.R.integer;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.TextView;

public class Profil extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_profil);
		
		TextView nutzerName = (TextView) findViewById(R.id.userName);
		
		TextView anzahlFragen = (TextView) findViewById(R.id.anzahlFragen);
		
		TextView fragenRichtig = (TextView) findViewById(R.id.fragenRichtig);
		
		
		Benutzer benutzer = AppVariabels.user;
		
		nutzerName.setText(benutzer.getBenutzername());
		
		
		anzahlFragen.setText(String.valueOf(benutzer.getFregenEingestellt()));
		
		fragenRichtig.setText(String.valueOf(benutzer.getRichtig_beantwortete_Fragen()));
		
        ImageView iv = (ImageView)findViewById(R.id.ivProfilPic);
        URL img_value = null;
        JSONObject response = null;
        String json_data = "";
        try {
			response = Util.parseJson(AppVariabels.facebook.request("/me", new Bundle(), "GET"));

			 json_data = response.getString("id");
        } catch (FacebookError e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (MalformedURLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
        try {
        	if (json_data!=""){
           img_value = new URL("http://graph.facebook.com/"+ json_data +"/picture?type=small");
        	}
       } catch (MalformedURLException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
        Bitmap mIcon1 = null;
       try {
           mIcon1 = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
           iv.setImageBitmap(mIcon1);
       } catch (IOException e) {
          
       }

	}


}
